package com.dx.financehy.modules.touristMessages.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dx.financehy.modules.system.domain.dto.BaseDTO;
import com.dx.financehy.modules.touristMessages.domain.TouristMessages;
import com.dx.financehy.modules.touristMessages.domain.dto.AddTouristMessagesDTO;
import com.dx.financehy.modules.touristMessages.mapper.TouristMessagesMapper;
import com.dx.financehy.modules.touristMessages.service.TouristMessagesService;
import com.dx.financehy.utils.security.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: huang_jigao
 * @Title: TouristMessagesImpl
 * @ProjectName: financehy
 * @Description:
 * @date: 2024-05-16 10:56
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TouristMessagesImpl extends ServiceImpl<TouristMessagesMapper, TouristMessages> implements TouristMessagesService {

    /**
     * 新增留言
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTouristMessages(AddTouristMessagesDTO dto){
        TouristMessages touristMessages = new TouristMessages();
        BeanUtils.copyProperties(dto,touristMessages);
        touristMessages.setMsgUserPhone(SecurityUtils.getCurrentUser().getUsername());
        touristMessages.setCreateBy(dto.getUserId());
        touristMessages.setCreateTime(new Date());
        baseMapper.addTouristMessages(touristMessages);
    }

    /**
     * 查看留言
     *
     * @return
     */
    @Override
    public List<TouristMessages> getTouristMessages(){
        String userId = SecurityUtils.getCurrentUserId();
        return baseMapper.getAdminTouristMessages(userId);
    }

    /**
     * 删除留言
     *
     * @param req
     */
    @Override
    public void delTouristMessages(BaseDTO req){

        String userId = SecurityUtils.getCurrentUserId();
        Date updateTime = new Date();

        LambdaUpdateWrapper<TouristMessages> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TouristMessages::getDeleted,true)
                .set(TouristMessages::getUpdateBy,userId)
                .set(TouristMessages::getUpdateTime,updateTime)
                .eq(TouristMessages::getId,req.getId());

        baseMapper.update(null,wrapper);
    }

    /**
     * 查看留言详情
     *
     * @return
     */
    @Override
    public TouristMessages getTouristMessagesDetail(String messagesid) {
        LambdaQueryWrapper<TouristMessages> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TouristMessages::getId,messagesid).eq(TouristMessages::getDeleted,false);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 后台查看留言
     *
     * @return
     */
    @Override
    public List<TouristMessages> getAdminTouristMessages(){
        String userId = null;
        if (SecurityUtils.getCurrentUserType() == 3) {
            userId = SecurityUtils.getCurrentUserId();
        }
        return baseMapper.getAdminTouristMessages(userId);
    }

    /**
     * 后台查看留言详情
     *
     * @return
     */
    @Override
    public TouristMessages getAdminTouristMessagesDetail(String messagesid) {
        LambdaQueryWrapper<TouristMessages> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TouristMessages::getId,messagesid).eq(TouristMessages::getDeleted,false);
        return baseMapper.selectOne(queryWrapper);
    }
}
